<?php
include_once ('class.mssql.php');

class EditConfigman
{
    private $comtec_id_configman;
    private $configman_name;
    private $maintainment_period;
    private $maintainment_period_date;
    private $maintainment_day;
   
    private $errors;
    private $token;

    public function __construct()
    {
        $this->errors   = array();
	$this->comtec_id_configman              = $_POST['comtec_id_configman'];
        
	$this->configman_name			= $_POST['configman_name'];
	$this->maintainment_period		= $_POST['maintainment_period'];
	$this->maintainment_period_date		= $_POST['maintainment_period_date'];
        if(strlen($this->maintainment_period_date) > 400)
        {
         $this->maintainment_period_date = substr($this->maintainment_period_date,0,400);   
        }
        $this->maintainment_day                 = $_POST['maintainment_day'];
       
        $this->token				= $_POST['token'];
    }
    
    public function process()
    {
        if($this->valid_token() && $this->valid_data())
            $this->edit();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function edit()
    {
        $db = new MSSQL();
        
        $tsql  = "UPDATE configman SET ";
	$tsql .= " configman_name= ? , ";
        $tsql .= " maintainment_period= ? , ";
        $tsql .= " maintainment_period_date= ? , ";
        $tsql .= " maintainment_day= ? ";                        
	$tsql .= " WHERE comtec_id_configman= ? ";
        
        $params = array(&$this->configman_name,&$this->maintainment_period,&$this->maintainment_period_date,&$this->maintainment_day,
                        &$this->comtec_id_configman);
        $stmt = $db->prepare($tsql,$params);

        if(!sqlsrv_execute($stmt))
            $this->errors[] = 'No se puede procesar la edici&oacute;n';
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_data()
    {
        //if($this->user_exists())
          //  $this->errors[] = 'Direccion de correo electronico ya existe';

        return count($this->errors)? 0 : 1;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
